package product;

import java.sql.CallableStatement;
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import product.SqlServerConnectionUtil;

public class UserBalance {  
	
	public static void setUserBalance(String[] args) { 
		Connection conn=null;
		CallableStatement proc=null;

		Calendar cale = null;    
		cale = Calendar.getInstance();    
		// 获取当月第一天和最后一天    
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");    
		String firstday,lastday;  
		
		 // 获取前月的第一天    
		cale = Calendar.getInstance();    
		cale.add(Calendar.MONTH, 0);    
		cale.set(Calendar.DAY_OF_MONTH, 1);    
		firstday = format.format(cale.getTime());    
		// 获取前月的最后一天    
		cale = Calendar.getInstance();    
		cale.add(Calendar.MONTH, 1);    
		cale.set(Calendar.DAY_OF_MONTH, 0);    
		lastday = format.format(cale.getTime());    
		System.out.println("本月第一天和最后一天分别是 ： " + firstday + " and " + lastday); 
		String startdate=firstday+" 00:00:00";
		String endDate=lastday+" 23:59:59";
		String sql="{call user_balance(?,?)}";
		System.out.println("本月第一天startdate和最后一天endDate分别是 ： " + startdate + " and " + endDate);
		try {
			conn = SqlServerConnectionUtil.getConnection();
			proc=conn.prepareCall(sql);
            proc.setString(1, startdate);
            proc.setString(2, endDate);    
            //执行
            proc.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(conn!=null){
                try {
                	conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(proc!=null){
                try {
                    proc.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
  
	}  

}


